﻿Imports System.IO
Imports System.ComponentModel
Public Class dsCLB
    'Khoi tao chi co Ma va Ten
    Dim path As String = Directory.GetCurrentDirectory
    Dim hinh As New LayHinh
    Private _maCLB As Integer
    Private _tenCLB As String
    Private _TenSan As String
    Private _TenHLV As String
    Private _IMG As Image

    Sub New()
        _maCLB = -1
        _tenCLB = ""
        _TenSan = ""
        _TenHLV = ""
        _IMG = Nothing
    End Sub

    Public Property MaCLB() As Integer
        Get
            Return _maCLB
        End Get
        Set(value As Integer)
            _maCLB = value
        End Set
    End Property

    Public Property TenCLB As String
        Get
            Return _tenCLB
        End Get
        Set(value As String)
            _tenCLB = value
        End Set
    End Property

    Public Property TenSan() As String
        Get
            Return _TenSan
        End Get
        Set(value As String)
            _TenSan = value
        End Set
    End Property

    Public Property HLV As String
        Get
            Return _TenHLV
        End Get
        Set(value As String)
            _TenHLV = value
        End Set
    End Property

    Public Property IMG As Image
        Get
            Return _IMG
        End Get
        Set(value As Image)
            _IMG = value
        End Set
    End Property

    Dim context As New QLDBDataContext

    'Function ResizImg(ByVal img As Image, width As Integer, height As Integer)
    '    Dim bm As New Bitmap(width, height)
    '    Dim gr = Graphics.FromImage(bm)
    '    gr.DrawImage(img, 0, 0, width, height)
    '    Return bm
    'End Function

    'lay tat ca ma, ten clb 

    Public Function LayDoiBong() As List(Of dsCLB)
        Dim table = From tb1 In context.CAULACBOs
                    Join tb2 In context.HUANLUYENVIENs
                    On tb1.MaHLV Equals tb2.MaHLV
                    Join tb3 In context.SANVDs
                    On tb1.MaSan Equals tb3.MaSan
                    Select New dsCLB With {.MaCLB = tb1.MaCLB, .IMG = hinh.ResizImg(path + "\images\caulacbo\" + tb1.LOGO, 40, 30), .TenCLB = tb1.TenCLB, .TenSan = tb3.TenSan, .HLV = tb2.TenHLV}

        Return table.ToList()

    End Function
    'lay tat ca ma,ten clb co them ALL
    Public Function LayDoiBongTK() As IList
        Dim list As BindingList(Of CAULACBO)
        list = context.CAULACBOs.GetNewBindingList()
        list.Insert(0, New CAULACBO With {.MaCLB = -1, .TenCLB = "ALL"})
        Return list
    End Function

    Public Function LayTenDoiBong(ByVal ma As Integer) As String
        Dim query = (From p In context.CAULACBOs
                    Where p.MaCLB = ma
                    Select p.TenCLB).First
        Return query

    End Function

    Private Function LayTenDoi1(ByVal mt As Integer) As String
        Dim v = context.TRANDAUs.SingleOrDefault(Function(p) p.MaTran = mt)
        Return LayTenDoiBong(v.MaCLB1)
    End Function

    Private Function LayMaD1(ByVal mt As Integer) As Integer
        Dim v = context.TRANDAUs.SingleOrDefault(Function(p) p.MaTran = mt)
        Return v.MaCLB1
    End Function

    Private Function LayTenDoi2(ByVal mt As Integer) As String
        Dim v = context.TRANDAUs.SingleOrDefault(Function(p) p.MaTran = mt)
        Return LayTenDoiBong(v.MaCLB2)
    End Function

    Private Function LayMaD2(ByVal mt As Integer) As Integer
        Dim v = context.TRANDAUs.SingleOrDefault(Function(p) p.MaTran = mt)
        Return v.MaCLB2
    End Function

    Public Function LayDoi(ByVal MT As Integer) As List(Of dsCLB)
        Dim ds As New List(Of dsCLB)
        ds.Insert(0, New dsCLB With {.TenCLB = LayTenDoi1(MT), .MaCLB = LayMaD1(MT)})
        ds.Insert(1, New dsCLB With {.TenCLB = LayTenDoi2(MT), .MaCLB = LayMaD2(MT)})
        Return ds
    End Function

    

    Public Function TimKiem(ByVal TenDoi As String, ByVal MaSanVD As Integer, ByVal MaHLV As Integer) As List(Of dsCLB)
        If TenDoi = "" Then
            Dim q = From clb In context.CAULACBOs Where clb.MaSan = MaSanVD Or clb.MaHLV = MaHLV
                 Join hlv In context.HUANLUYENVIENs On hlv.MaHLV Equals clb.MaHLV
                 Join svd In context.SANVDs On svd.MaSan Equals clb.MaSan
                Select New dsCLB With {.MaCLB = clb.MaCLB, .IMG = hinh.ResizImg(path + "\images\caulacbo\" + clb.LOGO, 20, 20), .TenCLB = clb.TenCLB, .TenSan = svd.TenSan, .HLV = hlv.TenHLV}
            Return q.ToList()
        Else
            Dim q = From clb In context.CAULACBOs Where clb.TenCLB Like TenDoi Or clb.MaSan = MaSanVD And clb.MaHLV = MaHLV
                 Join hlv In context.HUANLUYENVIENs On hlv.MaHLV Equals clb.MaHLV
                 Join svd In context.SANVDs On svd.MaSan Equals clb.MaSan
                Select New dsCLB With {.MaCLB = clb.MaCLB, .IMG = hinh.ResizImg(path + "\images\caulacbo\" + clb.LOGO, 20, 20), .TenCLB = clb.TenCLB, .TenSan = svd.TenSan, .HLV = hlv.TenHLV}
            Return q.ToList()
        End If
    End Function


End Class
